主题
下载文件扩展 - HttpDownloadFileEx
函数简介
下载文件,支持断点续传、进度回调、重试和超时设置。
接口名称
HttpDownloadFileExDLL调用
c
int32_t HttpDownloadFileEx(int64_t instance, const char* url, const char* save_path, DownloadCallback callback, int64_t user_data, int32_t max_retries, int32_t connect_timeout_sec, int32_t read_timeout_sec);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| url | 字符串 | 完整URL。 |
| save_path | 字符串 | 本地保存路径。 |
| callback | DownloadCallback | 下载进度回调函数。 |
| user_data | 长整数型 | 传给callback的用户数据。 |
| max_retries | 整数型 | 断线后最大重试次数。 |
| connect_timeout_sec | 整数型 | 连接超时秒数,0使用默认值。 |
| read_timeout_sec | 整数型 | 读取超时秒数,0使用默认值。 |
使用场景
| 场景 | 推荐参数 |
|---|---|
| 大文件下载 | max_retries=3~5,read_timeout_sec=60~120 |
| 不稳定网络 | 增大 max_retries 和超时 |
| 内网快速下载 | 超时传 0 使用默认值 |
与 HttpDownloadFile 的差异:Ex 版额外支持 断线自动重试 与 连接/读取超时 配置。
示例
c
void DownloadCallback(int64_t current, int64_t total, int64_t speed, int64_t user_data);| 参数名 | 类型 | 说明 |
|---|---|---|
| current | 长整数型 | 已下载字节数 |
| total | 长整数型 | 总字节数(0表示未知) |
| speed | 长整数型 | 当前下载速度(字节/秒) |
| user_data | 长整数型 | 由调用方传入的用户数据 |
示例
SDK 调用
cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
// max_retries=3, connect_timeout=10s, read_timeout=60s
int ret = ola.HttpDownloadFileEx(
"https://example.com/large.zip",
"D:\\downloads\\large.zip",
nullptr, 0,
3, 10, 60
);
if (ret == 0) {
// 大文件/不稳定网络推荐 Ex 版
}csharp
using OLAPlug;
var ola = new OLAPlugServer();
int ret = ola.HttpDownloadFileEx("https://example.com/large.zip", @"D:\downloads\large.zip", null, 0, 3, 10, 60);python
from OLAPlugServer import OLAPlugServer
ola = OLAPlugServer()
ret = ola.HttpDownloadFileEx("https://example.com/large.zip", r"D:\downloads\large.zip", None, 0, 3, 10, 60)java
import com.olaplug.OLAPlugServer;
OLAPlugServer ola = new OLAPlugServer();
int ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\\downloads\large.zip", null, 0, 3, 10, 60);go
import "github.com/ola/olaplug/olaplug"
ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
ret := ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\\downloads\large.zip", nil, 0, 3, 10, 60)rust
use olaplug::OLAPlugServer;
let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let ret = ola.http_download_file_ex("https://example.com/large.zip", "D:\\downloads\large.zip", None, 0, 3, 10, 60);cpp
var ola = com("OlaPlug.OlaSoft")
var ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\\downloads\large.zip", 0, 0, 3, 10, 60)vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\downloads\large.zip", 0, 0, 3, 10, 60)text
.局部变量 ola, OLAPlug
ola.创建 ()
ret = ola.HttpDownloadFileEx ("https://example.com/large.zip", "D:\downloads\large.zip", 0, 0, 3, 10, 60)aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\downloads\large.zip", null, 0, 3, 10, 60);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\downloads\large.zip", 0, 0, 3, 10, 60)cpp
#include "OLAPlugServer.h"
OLAPlugServer ola;
int ret = ola.HttpDownloadFileEx("https://example.com/large.zip", "D:\\downloads\large.zip", nullptr, 0, 3, 10, 60);原生 DLL 调用
cpp
long instance = CreateCOLAPlugInterFace();
int ret = HttpDownloadFileEx(instance, "https://example.com/large.zip", "D:\\downloads\\large.zip", 0, 0, 3, 10, 60);csharp
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int ret = HttpDownloadFileEx(instance, "https://example.com/large.zip", "D:\\downloads\\large.zip", 0, 0, 3, 10, 60);python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int ret = HttpDownloadFileEx(instance, "https://example.com/large.zip", "D:\\downloads\\large.zip", 0, 0, 3, 10, 60);返回值
| 返回值 | 说明 |
|---|---|
0 | 成功。 |
| < 0 | 失败,错误码与相关接口一致。 |
注意事项
| 项目 | 说明 |
|---|---|
| 支持断点续传和自动重试 | 支持断点续传和自动重试。 |
| 重试次数不包括首次尝试 | 重试次数不包括首次尝试。 |
| 默认值 | 超时设置为0时使用默认值(通常为30秒)。 |
| 适合下载大文件或网络不稳定的场景 | 适合下载大文件或网络不稳定的场景。 |
